<template>
	<om-page :title="title">
		<view class="om_hd_fix" style="-webkit-box-shadow: 0px 1px 10px #b3b2b2;">
			<om-sub :title="getTitle" >
				<view slot="extra"><button type="primary" size="mini" @click="finish">完成</button></view>
			</om-sub>
		</view>
		<view class="page__bd" style="padding-top: 100rpx;"></view>
		<uni-list>
			<uni-list-item :showArrow="false" :showHover="false" :showClear="showClear" @iconClear="clearFunc">
				<textarea
					:maxlength="max" 
					auto-height 
					style="min-height: 50px;"
					class="margin-xl" 
					v-model="remark" 
					:focus="focus"
					:disabled="disabled"
					:placeholder="getPrompt"/>
			</uni-list-item>
		</uni-list>
		<view class="flex justify-end padding-right-sm"><label>{{getMax}}</label></view>
	</om-page>
</template>

<script>
	export default {
		data() {
			return {
				title:"",
				parentfieldname:"",
				fieldname:"",
				remark:"",
				max:-1,
				isref:false,
				focus:false,
				disabled:false,
				showClear:true,
				placeholder:'请输入'
			}
		},
		computed:{
			getTitle(){
				if(~this.max){
					return this.title
				}else {
					if(this.remark){
						return this.title + "["+this.remark.length+"]"
					}else{
						return this.title;
					}
				}
			},
			getMax(){
				if(~this.max){
					return this.remark.length + "/" + this.max
				} else {
					return ""
				}
			},
			getPrompt(){
				if(!this.disabled){
					return this.placeholder
				}else{
					return this.remark
				}
				
			},
			
		},
		methods: {
			finish() {
				let _that = this
				let pages = getCurrentPages(); 
				if(pages.length >= 2) {
					let prePage = pages[pages.length-2]
					if(this.isref) {
						//#ifdef APP-PLUS
						prePage['$vm']['$refs']['autoform']['formdata'][_that.fieldname] = _that.remark
						//#endif
						//#ifndef APP-PLUS
						for(var iref in prePage.$refs) {
							var autoform = prePage.$refs[iref]
							if(autoform.formdata && autoform.formdata.hasOwnProperty(_that.fieldname)){
								autoform.formdata[_that.fieldname] = _that.remark
								break;
							}
						}
						//#endif
					} else {
						if(_that.parentfieldname){
							//#ifdef APP-PLUS
							prePage['$vm'][_that.parentfieldname][_that.fieldname] = _that.remark
							//#endif
							
							//#ifndef APP-PLUS
							prePage[_that.parentfieldname][_that.fieldname] = _that.remark
							//#endif
						} else {
							prePage[_that.fieldname] = _that.remark
						}
					}
					uni.navigateBack()
				}else{
					// this.om.toast("页面异常")
				}
			},
			clearFunc(){
				this.remark = ""
				let _that = this
				_that.focus = false
				this.$nextTick(function(){
					_that.focus = true
				})
			}
		},
		onLoad(options) {
			this.title = options.title||"备注"
			uni.setNavigationBarTitle({
			  title: this.title
			})
			if(!options.fieldname) {//缓存名称
				uni.navigateBack()
			}
			this.fieldname = options.fieldname
			if(options.parentfieldname) {//缓存名称
				this.parentfieldname = options.parentfieldname
			}
			if(options.isref){//缓存名称
				this.isref = options.isref
			}
			if(options.max){
				this.max = parseInt(options.max)
			}
			if(options.remark){
				this.remark = options.remark
			}
			if(options.placeholder){
				this.placeholder = options.placeholder
			}
			this.disabled = options.disabled==="true" ? true:false
			this.showClear = options.showClear === "false"? false:true
			let _that = this   
			this.$nextTick(function(){
				_that.focus = true
			})
		}
	}
</script>

<style>

</style>
